﻿using System.Data;
using FastReport;
using FastReport.Data;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using FastReport.Export;
using System;

namespace _180FrameWork.ReportExtend
{
    public class FastReportHelper
    {   
        #region 初始化

        static FastReportHelper()
        {
            Init();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public static void Init()
        {
            //动态加载语言包
            DirectoryInfo di = new DirectoryInfo(ReportUtils.GetAppPath() + @"rpt\localization");
            if (di.Exists)
            {
            }
            else
            {
                di.Create();
            }
            FastReport.Utils.Res.LocaleFolder = di.FullName;
            FileInfo fi = new FileInfo(ReportUtils.GetAppPath() + @"rpt\localization\Chinese (Simplified).frl");
            if (fi.Exists)
            {
                FastReport.Utils.Res.LoadLocale(fi.FullName);
            }
        }

        #endregion

   

        #region dataset数据源
        /// <summary>
        /// 获取报表对象
        /// </summary>
        /// <param name="filepatch"></param>
        /// <param name="ds"></param>
        /// <returns></returns>
        private static Report GetReport(string filepatch, DataSet ds)
        {
            Report rpt = new Report();
            rpt.Load(filepatch);
            rpt.RegisterData(ds, ds.DataSetName);          
            rpt.Prepare();
            return rpt;
        }
        /// <summary>
        /// 打印报表
        /// </summary>
        /// <param name="filepatch">报表模板地址</param>
        /// <param name="ds">数据源</param>
        /// <param name="printer">打印机</param>
        /// <param name="ptype">0=直接打印，1=选择打印机后打印</param>
        /// <returns></returns>
        public static bool Print(string filepatch, DataSet ds, string printer, int ptype = 0)
        {
            Report rpt = GetReport(filepatch, ds);
            rpt.PrintSettings.Printer = printer;
            if (ptype == 0)
                rpt.PrintSettings.ShowDialog = false;
            rpt.Print();
            return true;
        }
        /// <summary>
        /// 展示报表
        /// </summary>
        /// <param name="filepatch"></param>
        /// <param name="ds"></param>
        /// <param name="printer"></param>
        public static void Show(string filepatch, DataSet ds)
        {
            Report rpt = GetReport(filepatch, ds);
            rpt.Show();
        }
        /// <summary>
        /// 导出报表
        /// </summary>
        /// <param name="filepatch"></param>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static Stream Export(string filepatch, DataSet ds, string ExportType)
        {
            Report rpt = GetReport(filepatch, ds);
            ExportBase e = null;
            switch (ExportType.ToUpper())
            {
                case "HTML":
                    e = new FastReport.Export.Html.HTMLExport();
                    break;
                case "PDF":
                    var PDF = new FastReport.Export.Pdf.PDFExport();
                    PDF.EmbeddingFonts = false;
                    e = PDF;
                    break;
                case "EXCEL":
                    e = new FastReport.Export.OoXML.Excel2007Export();
                    break;
                case "WORD":
                    e = new FastReport.Export.OoXML.Word2007Export();
                    break;
                case "MHT":
                default:
                    e = new FastReport.Export.Mht.MHTExport();
                    break;
            }
            MemoryStream s = new MemoryStream();
            rpt.Report.Export(e, s);
            rpt.Dispose();
            e.Dispose();
            s.Position = 0;
            return s;
        }
        /// <summary>
        /// 导出报表到文件
        /// </summary>
        /// <param name="filepatch"></param>
        /// <param name="ds"></param>
        /// <param name="ExportType"></param>
        /// <param name="Path"></param>
        public static void Export(string filepatch, DataSet ds, string ExportType, string Path)
        {
            Report rpt = GetReport(filepatch, ds);
            ExportBase e = null;
            switch (ExportType.ToUpper())
            {
                case "HTML":
                    e = new FastReport.Export.Html.HTMLExport();
                    break;
                case "PDF":
                    var PDF = new FastReport.Export.Pdf.PDFExport();
                    PDF.EmbeddingFonts = false;
                    e = PDF;
                    break;
                case "EXCEL":
                    e = new FastReport.Export.OoXML.Excel2007Export();
                    break;
                case "WORD":
                    e = new FastReport.Export.OoXML.Word2007Export();
                    break;
                case "MHT":
                default:
                    e = new FastReport.Export.Mht.MHTExport();
                    break;
            }
            rpt.Report.Export(e, Path);
            rpt.Dispose();
            e.Dispose();
        }
        /// <summary>
        /// 修改报表
        /// </summary>
        /// <param name="filepatch"></param>
        /// <param name="ds"></param>
        /// <param name="ReSetDS"></param>
        public static void Edit(string filepatch, DataSet ds, bool ReSetDS = false)
        {
            Report rpt = new Report();
            rpt.Load(filepatch);
            if (ReSetDS)
            {
                rpt.Dictionary.DataSources.Clear();
                rpt.Dictionary.RegisterData(ds, ds.DataSetName, true);
            }
            //rpt.RegisterData(ds);
            //rpt.Prepare();
            rpt.AutoFillDataSet = true;
            rpt.PrintSettings.SavePrinterWithReport = true;
            rpt.Design();
        }
        /// <summary>
        /// 新建报表
        /// </summary>
        /// <param name="ds"></param>
        public static void New(DataSet ds)
        {
            Report rpt = new Report();

            rpt.Dictionary.RegisterData(ds, ds.DataSetName, true);
            rpt.Prepare();
            rpt.PrintSettings.SavePrinterWithReport = true;
            //rpt.PrintSettings.Printer = printer;
            rpt.Design();
        }

        #endregion

        #region IEnumerable数据源
        /// <summary>
        /// 获取报表对象
        /// </summary>
        /// <param name="filepatch"></param>
        /// <param name="ds"></param>
        /// <returns></returns>
        private static Report GetReport(string filepatch, IEnumerable ds)
        {
            Report rpt = new Report();
            rpt.Load(filepatch);

            rpt.RegisterData(ds, "BusinessObject");

            rpt.Prepare();
            rpt.PrintSettings.SavePrinterWithReport = true;
            return rpt;
        }
        /// <summary>
        /// 打印报表
        /// </summary>
        /// <param name="filepatch">报表模板地址</param>
        /// <param name="ds">数据源</param>
        /// <param name="printer">打印机</param>
        /// <param name="ptype">0=直接打印，1=选择打印机后打印</param>
        /// <returns></returns>
        public static bool Print(string filepatch, IEnumerable ds, string printer, int ptype = 0)
        {
            Report rpt = GetReport(filepatch, ds);
            rpt.PrintSettings.Printer = printer;
            if (ptype == 0)
                rpt.PrintSettings.ShowDialog = false;
            rpt.Print();
            return true;
        }
        /// <summary>
        /// 展示报表
        /// </summary>
        /// <param name="filepatch"></param>
        /// <param name="ds"></param>
        /// <param name="printer"></param>
        public static void Show(string filepatch, IEnumerable ds)
        {
            Report rpt = GetReport(filepatch, ds);
            rpt.Show();
        }
        private static ExportBase GetExportBase(string ExportType)
        {
            ExportBase e = null;
            switch (ExportType.ToUpper())
            {
                case "HTML":
                    e = new FastReport.Export.Html.HTMLExport();
                    break;
                case "PDF":
                    var PDF = new FastReport.Export.Pdf.PDFExport();

                    PDF.EmbeddingFonts = true;
                    PDF.JpegQuality = 50;
                    PDF.RichTextQuality = 50;
                    PDF.Background = false;//背景
                    PDF.PrintOptimized = false;//打印选项
                    //PDF.AllowCopy = false;//拷贝
                    //PDF.AllowModify = false;//修改
                    //PDF.AllowOpenAfter = false;//导出后打开
                    PDF.AllowAnnotate = false;//注释
                    //PDF.CenterWindow = false;
                    PDF.Compressed = true;
                    //PDF.DisplayDocTitle = false;
                    e = PDF;
                    break;
                case "EXCEL":
                    e = new FastReport.Export.OoXML.Excel2007Export();
                    break;
                case "WORD":
                    e = new FastReport.Export.OoXML.Word2007Export();
                    break;
                case "MHT":
                default:
                    e = new FastReport.Export.Mht.MHTExport();
                    break;
            }
            return e;
        }
        /// <summary>
        /// 导出报表
        /// </summary>
        /// <param name="filepatch"></param>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static Stream Export(string filepatch, IEnumerable ds, string ExportType)
        {
            Report rpt = GetReport(filepatch, ds);
            ExportBase e = GetExportBase(ExportType);
            MemoryStream s = new MemoryStream();
            rpt.Report.Export(e, s);
            rpt.Dispose();
            e.Dispose();
            s.Position = 0;
            return s;
        }
        /// <summary>
        /// 导出报表到文件
        /// </summary>
        /// <param name="filepatch"></param>
        /// <param name="ds"></param>
        /// <param name="ExportType"></param>
        /// <param name="Path"></param>
        public static void Export(string filepatch, IEnumerable ds, string ExportType, string Path)
        {
            Report rpt = GetReport(filepatch, ds);
            ExportBase e = GetExportBase(ExportType);
            rpt.Report.Export(e, Path);
            rpt.Dispose();
            e.Dispose();
        }
        /// <summary>
        /// 编辑报表
        /// </summary>
        /// <param name="filepatch"></param>
        /// <param name="ds"></param>
        /// <param name="ReSetDS"></param>
        public static void Edit(string filepatch, IEnumerable ds, bool ReSetDS = false)
        {
            Report rpt = new Report();
            rpt.Load(filepatch);
            if (ReSetDS)
            {
                rpt.Dictionary.DataSources.Clear();
                rpt.Dictionary.RegisterBusinessObject(ds, "BusinessObject", 10, true);
            }
            rpt.RegisterData(ds, "BusinessObject");
            rpt.AutoFillDataSet = true;
            rpt.PrintSettings.SavePrinterWithReport = true;
            rpt.Design();
        }
        /// <summary>
        /// 新建报表
        /// </summary>
        /// <param name="ds"></param>
        public static void New(IEnumerable ds)
        {
            Report rpt = new Report();
            //foreach (string Key in ds.Keys)
            //ds.TryAlignToInterface<string>();
            rpt.Dictionary.RegisterBusinessObject(ds, "BusinessObject", 10, true);
            //rpt.Dictionary.RegisterData(ds[Key], Key, true);

            rpt.Prepare();
            rpt.PrintSettings.SavePrinterWithReport = true;
            rpt.Design();
        }

        #endregion

    }
}
